﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OnlineManagement.BLL;
using System.Data;
namespace WebAppTemplate
{
    public partial class SowManagement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                HttpCookie cookie = new HttpCookie(OnlineManagement.Common.PubConstant.HoggeryIdCookieName, "1001");
                Response.Cookies.Add(cookie);

                if (Response.Cookies.Get(OnlineManagement.Common.PubConstant.HoggeryIdCookieName) != null)
                {
                    string hoggeryIdStr = Response.Cookies.Get(OnlineManagement.Common.PubConstant.HoggeryIdCookieName).Value;
                    int hoggeryId = 0;
                    if (int.TryParse(hoggeryIdStr, out hoggeryId))
                    {
                       // BindRepeater(hoggeryId);
                       // BindDropDownlist(hoggeryId);
                    }
                }
                //if (Request.QueryString["id"] != null)
                //{
                //    string hoggeryIdStr = Request.QueryString["id"].ToString();
                //    int hoggeryId = 0;
                //    if (int.TryParse(hoggeryIdStr, out hoggeryId))
                //    {
                //        BindRepeater(hoggeryId);
                //        BindDropDownlist(hoggeryId);
                //    }
                //}
            }
            
        }

        private void BindRepeater(int hoggeryId)
        {
            try
            {
                Sow sow = new Sow();
                int currentPage = 1;
                DataSet ds = sow.GetSowsByProc(hoggeryId, currentPage);
                if (ds != null && ds.Tables != null && ds.Tables.Count > 0)
                {
                    rpSow.DataSource = ds;
                    rpSow.DataBind();
                }
            }catch(Exception){
                
            }
            
        }

        private void BindDropDownlist(int hoggeryId)
        {
            try
            {
                DataSet ds= OnlineManagement.Common.DataCache.GetHogBaseInfo();
                if (ds == null)
                {
                    ProcedureHandle handle = new ProcedureHandle();
                    //return 3 tables,1:PigSource,2:PropagateState,3:PigBreed,4:PigPen,5:pigCategory,6:GrowPhase
                    ds = handle.GetPigBaseInfoProcedure(hoggeryId);
                    OnlineManagement.Common.DataCache.CacheHogBaseInfo(ds);
                }
                Bind(ds);
            }catch(Exception){
                
            }
        }

        private void Bind(DataSet ds)
        {
            if (ds != null && ds.Tables != null)
            {
                ddlSowScource.DataSource = ds.Tables[0];
                ddlSowScource.DataTextField = "Name";
                ddlSowScource.DataValueField = "ID";
                ddlSowScource.DataBind();

                ddlPropagateState.DataSource = ds.Tables[1];
                ddlPropagateState.DataTextField = "Name";
                ddlPropagateState.DataValueField = "ID";
                ddlPropagateState.DataBind();

                ddlSowType.DataSource = ds.Tables[2];
                ddlSowType.DataTextField = "Name";
                ddlSowType.DataValueField = "ID";
                ddlSowType.DataBind();

                ddlPigPen.DataSource = ds.Tables[3];
                ddlPigPen.DataTextField = "Name";
                ddlPigPen.DataValueField = "ID";
                ddlPigPen.DataBind();

              

            }
        }

        //private void CacheSowInfo(DataSet ds) {
        //    string cachKey = OnlineManagement.Common.PubConstant.CacheSowInfoName;
        //    object objModel = OnlineManagement.Common.DataCache.GetCache(cachKey);
        //    if(objModel==null){
        //        int modelCache = OnlineManagement.Common.ConfigHelper.GetConfigInt("DataCache");
        //        OnlineManagement.Common.DataCache.SetCache(cachKey, ds, DateTime.Now.AddMinutes(modelCache), TimeSpan.Zero);
        //    }
        //}
    }
}